package com.google.code.gwtmvpscaffold;

import static com.google.code.gwtmvpscaffold.SystemVariables.PATH;

import java.io.File;

/**
 * 08/09/2012
 * 
 * @author Ismet Ozalp
 */
public class Main {

  /**
   * @param args
   */
  public static void main(String[] args) {

    /**
     * Validate arguments
     */
    if (args.length == 1) {
      if (args[0].equalsIgnoreCase("--help") || args[0].equalsIgnoreCase("-h")) {
        printHelp();
      }
      /**
       * Use current directory as path
       */
    } else if (args.length == 2) {
      MVPScaffold mvpScaffold = new MVPScaffold(new Paths(args[1]), PATH, args[0], args[1]);
      mvpScaffold.useOnlyDefaultTemplates();
    } else if (args.length == 3) {
      new MVPScaffold(new Paths(args[1]), args[2], args[0], args[1]);
    } else if (args.length == 4) {
      if (args[0].equalsIgnoreCase("-f")) {
        File f = validateFileExists(args[1]);
        if (f != null) {
          new MVPScaffold(new Paths(args[3]), PATH, args[2], args[3]).changeSingleTemplate(f);
        } else {
          printFileNotExists(args[1]);
        }
      } else if (args[0].equalsIgnoreCase("-z")) {
        File f = validateFileExists(args[1]);
        if (f != null) {
          new MVPScaffold(new Paths(args[3]), PATH, args[2], args[3])
              .changeMultipleTemplatesUsingZipFile(f);
        } else {
          printFileNotExists(args[1]);
        }
      } else {
        printHelp();
      }
      /**
       * If path to scaffold exists
       */
    } else if (args.length == 5) {
      if (args[0].equalsIgnoreCase("-f")) {
        File f = validateFileExists(args[1]);
        if (f != null) {
          new MVPScaffold(new Paths(args[3]), args[4], args[2], args[3]).changeSingleTemplate(f);
        } else {
          printFileNotExists(args[1]);
        }
      } else if (args[0].equalsIgnoreCase("-z")) {
        File f = validateFileExists(args[1]);
        if (f != null) {
          new MVPScaffold(new Paths(args[3]), args[4], args[2], args[3])
              .changeMultipleTemplatesUsingZipFile(f);
        } else {
          printFileNotExists(args[1]);
        }
      } else {
        printHelp();
      }
    } else {
      printHelp();
    }
  }

  private static void printFileNotExists(String string) {
    System.out.println("File:" + string + " not exists!");
    printHelp();
  }

  private static File validateFileExists(String fpath) {
    File f = new File(fpath).exists() ? new File(fpath) : null;
    return f;
  }

  private static void printHelp() {
    String msg = "supposed to be an help msg";
    System.out.println(msg);
  }

}
